import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/login";
import Layout from "@/components/Layout";
import Home from "@/views/home";
import Member from "@/views/member";
import Goods from "@/views/goods";
import Staff from "@/views/staff";
import Supplier from "@/views/supplier";
import Test from '@/views/test'
Vue.use(VueRouter);

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/login",
      component: Login,
      meta:{title:'用户登录'}
    },
    {
      path: "/",
      component: Layout,
      redirect: "/home",
      children: [
        { path: "test", component: Test },
        { path: "home", component: Home },
        { path: "member", component: Member, meta: { title: "会员管理" } },
        { path: "goods", component: Goods, meta: { title: "商品管理" } },
        { path: "staff", component: Staff, meta: { title: "员工管理" } },
        {
          path: "supplier",
          component: Supplier,
          meta: { title: "供应商管理" },
        },
      ],
    },
  ],
});

// 路由拦截，没有登录不能访问其他页面
// 登录与否根据localstorage里面是否有token
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const token = localStorage.getItem("vue-msm-token");
  if (!token) return next('/login')
  next()
});

export default router;
